/**
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 2008-2009 WURFL-Pro srl
 */
package net.sourceforge.wurfl.core.handlers.matchers;

import net.sourceforge.wurfl.core.Constants;
import net.sourceforge.wurfl.core.handlers.Handler;
import net.sourceforge.wurfl.core.handlers.classifiers.FilteredDevices;
import net.sourceforge.wurfl.core.request.WURFLRequest;

/**
 * @author WURFL-PRO SRL, Rome, Italy
 * @version $Id: BlackBerryMatcher.java 1117 2009-03-20 10:07:30Z filippo.deluca $
 */
public class BlackBerryMatcher extends AbstractMatcher {

	public BlackBerryMatcher(Handler handler) {
		super(handler);
	}

	/**
	 * Parse version number ("2","3.2","3.3","3.5","3.6","3.7","4.") and return
	 * "blackberry_generic_ver2","blackberry_generic_ver3_sub2",
	 * "blackberry_generic_ver3_sub30", "blackberry_generic_ver3_sub50",
	 * "blackberry_generic_ver3_sub60","blackberry_generic_ver3_sub70" or
	 * "blackberry_generic_ver4" respectively
	 */
	protected String applyRecoveryMatch(WURFLRequest request, FilteredDevices filteredDevices) {
		String userAgent = request.getUserAgent();
		if (userAgent.startsWith("BlackBerry")) {

			int position = userAgent.indexOf('/');

			if (position != -1 && position + 4 <= userAgent.length()) {
				String version = userAgent.substring(position + 1, position + 4);

				if (version.startsWith("2.")) {
					return "blackberry_generic_ver2";
				}
				if (version.startsWith("3.2")) {
					return "blackberry_generic_ver3_sub2";
				}
				if (version.startsWith("3.3")) {
					return "blackberry_generic_ver3_sub30";
				}
				if (version.startsWith("3.5")) {
					return "blackberry_generic_ver3_sub50";
				}
				if (version.startsWith("3.6")) {
					return "blackberry_generic_ver3_sub60";
				}
				if (version.startsWith("3.7")) {
					return "blackberry_generic_ver3_sub70";
				}
				if (version.startsWith("4.")) {
					return "blackberry_generic_ver4";
				}

				if (logger.isWarnEnabled()) {
					logger.warn("No version matched, User-Agent: " + userAgent + " version: " + version);
				}
			}
		}
		return Constants.GENERIC;

	}

}
